//
//  UITextFeld+Ex.swift
//  aquariumstudio
//
//  Created by zj on 2022/10/15.
//  Copyright © 2022 LXF. All rights reserved.
//

import UIKit

public extension UITextField{
    func xx_setUserNameStyle(placeholderString:String = "手机号".i18n,keyboardType:UIKeyboardType = .numberPad,placeholderColor:UIColor = .gray){
        placeholder = placeholderString
        xx_setPlaceholderColor(placeholderColor: placeholderColor)
        autocapitalizationType = .none
        returnKeyType = .next
        self.keyboardType = keyboardType //.namePhonePad
        autocorrectionType = .no
    }
    func xx_setPasswordStyle(placeholderString:String = "密码".i18n){
        placeholder = placeholderString
        autocapitalizationType = .none
        returnKeyType = .join
        isSecureTextEntry = true
    }
    
    func xx_setLeftImage(image:UIImage,size:CGSize? = nil,s spacing:CGFloat? = nil){
        
        let leftImageView = UIImageView.init(image: image)
        
        let v = UIView.init(frame: leftImageView.frame)
        
        v.addSubview(leftImageView)
        
        var f = v.frame
        
        if let s = size {
            f.size = s
        }
        
        if let spa = spacing {
            f.size.width += spa
        }
        
        v.frame = f
        
        leftView = v
        
        leftViewMode = .always
    }
    
    func xx_setPlaceholderColor(placeholderColor:UIColor){
        guard let text = self.placeholder else{
            return
        }
        
        self.attributedPlaceholder = NSAttributedString.init(string: text, attributes: [NSAttributedString.Key.foregroundColor :placeholderColor])
//        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//        dict[NSForegroundColorAttributeName] = [UIColor redColor];
//        NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:self.placeholder attributes:dict];
//        [self setAttributedPlaceholder:attribute];
    }
    
}
